package com.william.xiaobujiantest.Tools;

import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.william.xiaobujiantest.R;
import com.william.xiaobujiantest.WeatherService;
import com.william.xiaobujiantest.Whats;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by william on 2017/5/8.
 */

public class DatetimeCalcTool {


    /**
     * 获取正确的日期
     * @return 字符串数组，能正确显示的日期
     */
    private static void getRealDate(String[] dateTimes,String[] realDateTime){
        realDateTime[0] = getRealMM(dateTimes[1]);
        realDateTime[1] = dateTimes[2];
        realDateTime[2] = getRealWW(dateTimes[0]);
    }

    /**
     * 获取正确的时间
     * @param dateTimes
     * @param realDateTime
     */
    private static void getRealTime(String[] dateTimes,String[] realDateTime){
        HashMap<String,String> allTime = getAllTime(dateTimes[3]);
        realDateTime[3] = allTime.get("hh");
        realDateTime[4] = allTime.get("mm");
        realDateTime[5] = allTime.get("shiDuan");
    }

//    /**
//     * 定时启动任务
//     * @param dateTimes
//     */
//    private void syncWeatherTask(Context context,String[] dateTimes){
//        String hhmm = dateTimes[3].substring(0,5);
////        Log.i(TAG, "syncWeatherTask: hhmm的值为："+hhmm);
//        if(hhmm.equals(Whats.SYNCWEATHERTIME)){
//            startSyncWeatherService(context);
//        }
//    }
//
//    /**
//     * 启动天气同步服务
//     * @param context
//     */
//    private void startSyncWeatherService(Context context){
//        Intent intent = new Intent(context, WeatherService.class);
//        context.startService(intent);
//    }

    /**
     * 经过处理得到能够正确显示的月份
     * @param MM
     * @return
     */
    private static String getRealMM(String MM){

        String month = "";

        switch (MM){
            case "Jan":
                month = "1";
                break;
            case "Feb":
                month = "2";
                break;
            case "Mar":
                month = "3";
                break;
            case "Apr":
                month = "4";
                break;
            case "May":
                month = "5";
                break;
            case "Jun":
                month = "6";
                break;
            case "Jul":
                month = "7";
                break;
            case "Aug":
                month = "8";
                break;
            case "Sep":
                month = "9";
                break;
            case "Oct":
                month = "10";
                break;
            case "Nov":
                month = "11";
                break;
            case "Dec":
                month = "12";
                break;
        }
        return month;
    }

    /**
     * 经过处理得到能够正确显示的星期
     * @param WW
     * @return
     */
    private static String getRealWW(String WW){

        String weekday = "";

        switch (WW){
            case "Mon":
                weekday = "一";
                break;
            case "Tue":
                weekday = "二";
                break;
            case "Wed":
                weekday = "三";
                break;
            case "Thu":
                weekday = "四";
                break;
            case "Fri":
                weekday = "五";
                break;
            case "Sat":
                weekday = "六";
                break;
            case "Sun":
                weekday = "日";
                break;
        }
        return weekday;
    }

    /**
     * 经过处理得到能够正确显示的时间
     * @param time
     * @return 1.时 2.分 3.时段
     */
    private static HashMap getAllTime(String time){
        String[] hms = time.split(":");

        String hh = "";
        String mm = hms[1];
        String shiDuan = "";

        int h = Integer.parseInt(hms[0]);
        // 凌晨 早晨 上午 中午 下午 傍晚 夜晚 半夜
        if (h <= 12){

            hh = String.valueOf(h);

            if(h<3){
                shiDuan = "半夜";
            }
            if(h>=3 && h<5){
                shiDuan = "凌晨";
            }
            if(h>=5 && h<8){
                shiDuan = "早晨";
            }
            if(h>=8 && h<11){
                shiDuan = "上午";
            }
            if(h>=11){
                shiDuan = "中午";
            }
        } else {

            hh = String.valueOf(h-12);

            if(h<=14 ){
                shiDuan = "中午";
            }
            if(h>=14 && h<17){
                shiDuan = "下午";
            }
            if(h>=17 && h<20){
                shiDuan = "傍晚";
            }
            if(h>=20 && h<23){
                shiDuan = "夜晚";
            }
            if(h>=23){
                shiDuan = "半夜";
            }
        }
        HashMap allTime = new HashMap<String,String>();
        allTime.put("hh",hh);
        allTime.put("mm",mm);
        allTime.put("shiDuan",shiDuan);
        return allTime;
    }

    /**
     * 得到显示正确的日期和时间
     * @param context
     * @param dateTimes
     * @return
     */
    private static String[] getRealDateAndTime(Context context,String[] dateTimes){

//        HashMap<String,String> realDateTime = new HashMap<String, String>();
        String[] realDateTime = new String[6];//0:月 1:日 2:周几 3:时 4:分 5:时段
        getRealDate(dateTimes,realDateTime);
        getRealTime(dateTimes,realDateTime);
        return realDateTime;
    }
    /**
     * 主同步方法，各项同步基本都在此执行
     * @param context
     */
    public static String[] sync(Context context){
        String dt = (new Date()).toString();
        String[] dateTimes = dt.split(" ");
        String[] datesAndTimes = getRealDateAndTime(context,dateTimes);
        RemoteViews rvs = new RemoteViews(context.getPackageName(), R.layout.widget_weather_clock);
        UpdeWidgetTool.updateDateTime(context,rvs,datesAndTimes);
        return dateTimes;
    }
}
